﻿Namespace GUI

    Friend Class MirButton
        Implements Windows.GUIObj

        Dim mWnd As MirWindow

        Dim mName As String
        Dim mNormalColor As Color
        Dim mHoverColor As Color
        Dim mDownColor As Color
        Dim mBorderColor As Color
        Dim mTextColor As Color

        Dim mText As String
        Dim mTextBig As Boolean

        Dim mToolTipText As String

        Dim mPosX As Integer, mPosY As Integer, mPosZ As Single
        Dim mWidth As Integer, mHeight As Integer

        Dim mClientRect As Rectangle
        Dim mIsVisible As Boolean
        Dim mGetsFocus As Boolean

        Public Event Click()

        Public Sub New(ByRef wnd As MirWindow, ByRef name As String, ByRef x As Integer, ByRef y As Integer, ByRef w As Integer, ByVal h As Integer, ByRef z As Single, _
                         ByVal normalcolor As Color, ByVal hovercolor As Color, ByVal downcolor As Color, ByVal textcolor As Color, ByVal bdrcolor As Color, _
                        Optional ByRef text As String = "", Optional ByRef getsfocus As Boolean = True, Optional textBig As Boolean = False)

            mWnd = wnd
            mName = name
            mPosX = x
            mPosY = y
            mPosZ = z

            mWidth = w
            mHeight = h

            mNormalColor = normalcolor
            mHoverColor = hovercolor
            mDownColor = downcolor
            mBorderColor = bdrcolor
            mTextColor = textcolor

            mText = text
            mTextBig = textBig

            CalcClientRect()

            mIsVisible = True

            mGetsFocus = getsfocus

            wnd.AddInterfaceItem(Me)

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Implements Windows.mWindows.GUIObj.MouseInput

            Select Case type

                Case Mouse.MouseInputType.MouseClick
                    RaiseEvent Click()

            End Select

        End Sub

        Private Sub CalcClientRect()

            mClientRect = New Rectangle(mPosX, mPosY, mWidth, mHeight)

        End Sub

        Public Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean Implements Windows.mWindows.GUIObj.Contains

            If Not mIsVisible Then Return False
            If mClientRect.Contains(x, y) Then

                Return True

            End If

            Return False

        End Function

        Public Sub Draw(ByRef mode As DirectX.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Implements Windows.mWindows.GUIObj.Draw

            If Not mIsVisible Then Exit Sub

            If mode = DirectX.DrawingMode.Line Then

                If Mouse.IsOver(OffsetRect(mClientRect, x, y)) And mWnd Is Windows.GetWndAtPos(Mouse.XCoord, Mouse.YCoord) Then

                    If Mouse.bIsDown Then
                        'down
                        Engine.DrawSolidBox(x + mPosX, x + mPosX + mWidth, y + mPosY, y + mPosY + mHeight, mPosZ, mDownColor)
                    Else
                        'draw hover
                        Engine.DrawSolidBox(x + mPosX, x + mPosX + mWidth, y + mPosY, y + mPosY + mHeight, mPosZ, mHoverColor)
                    End If
                Else
                    'norm
                    Engine.DrawSolidBox(x + mPosX, x + mPosX + mWidth, y + mPosY, y + mPosY + mHeight, mPosZ, mNormalColor)
                End If

                Engine.DrawBox(x + mPosX, x + mPosX + mWidth, y + mPosY, y + mPosY + mHeight, mPosZ, 1.0F, mBorderColor)

            End If

            If mode = DirectX.DrawingMode.Text Then

                If mText.Length > 0 Then

                    If mTextBig Then
                        DirectX.ProFont9.DrawBoldText(mText, x + mPosX + (mWidth / 2) - (DirectX.ProFont9.GetBoldTextWidth(mText) / 2), y + mPosY + (mHeight / 2) - 5, mTextColor, mPosZ)
                    Else
                        DirectX.ProFont9.DrawNormalText(mText, x + mPosX + (mWidth / 2) - (DirectX.ProFont9.GetNormalTextWidth(mText) / 2), y + mPosY + (mHeight / 2) - 5, mTextColor, mPosZ)
                    End If

                End If

            End If

        End Sub

        Public Property Visible() As Boolean
            Get
                Return mIsVisible
            End Get
            Set(ByVal value As Boolean)
                mIsVisible = value
            End Set
        End Property

        Public Sub Focus() Implements Windows.mWindows.GUIObj.Focus

        End Sub

        Public Event TabSwitch() Implements Windows.mWindows.GUIObj.TabSwitch

        Public Sub UnFocus() Implements Windows.mWindows.GUIObj.UnFocus

        End Sub

        Public Sub Destroy() Implements Windows.mWindows.GUIObj.Destroy
            mWnd.RemoveInterfaceItem(Me)
        End Sub

        Public ReadOnly Property GetsFocus() As Boolean Implements Windows.mWindows.GUIObj.GetsFocus
            Get
                Return mGetsFocus
            End Get
        End Property

        Public Sub SetPos(ByRef x As Integer, ByRef y As Integer) Implements Windows.mWindows.GUIObj.SetPos
            mPosX = x
            mPosY = y
        End Sub

        Public ReadOnly Property ParentWin() As MirWindow Implements Windows.mWindows.GUIObj.ParentWin
            Get
                Return mWnd
            End Get
        End Property

        Public ReadOnly Property Name() As String Implements Windows.mWindows.GUIObj.Name
            Get
                Return mName
            End Get
        End Property

        Public Property Text() As String

            Get
                Return mText
            End Get

            Set(ByVal value As String)
                mText = value
            End Set

        End Property


        Public Function OffsetRect(ByRef rect As Rectangle, ByRef x As Integer, ByRef y As Integer) As Rectangle

            Dim tmp As Rectangle = rect
            tmp.Offset(x, y)

            Return tmp

        End Function

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.mKeyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyPressEventArgs) Implements Windows.mWindows.GUIObj.KeyboardInput

        End Sub
    End Class

End Namespace